package com.mzj.saas.mservice.property.repository;

import com.mzj.saas.mservice.property.entity.CommunityHouseUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hikari
 * @since 2021-05-25
 */
@Repository
public interface CommunityHouseUserRepository extends JpaRepository<CommunityHouseUser, Long>, JpaSpecificationExecutor<CommunityHouseUser> {
    List<CommunityHouseUser> findByUserIdAndStatus(String userId, Integer status);

    List<CommunityHouseUser> findByIdAndStatus(Long id, Integer status);


    List<CommunityHouseUser> findByUserId(String userId);

    List<CommunityHouseUser> findByMobileAndStatus(String mobile, Integer status);

    List<CommunityHouseUser> findByCommunityHouseIdAndStatus(String communityHouseId, Integer status);

    List<CommunityHouseUser> findByCommunityHouseIdAndStatusAndMobile(String communityHouseId, Integer status, String mobile);

    List<CommunityHouseUser> findByCommunityHouseIdAndStatusAndType(String communityHouseId, Integer status, Integer type);

    CommunityHouseUser findByCommunityHouseIdAndStatusAndCardNumberAndName(String communityHouseId, Integer status, String cardNumber, String name);

    List<CommunityHouseUser> findByCommunityHouseIdAndStatusAndAccountStatus(String communityHouseId, Integer status, Integer accountStatus);

    void deleteByCommunityHouseId(String communityHouseId);

    // 修改changeLabel
    @Modifying
    @Transactional
    @Query(value = "update saas_community_house_user schu set schu.change_label = ?1 where schu.community_house_id = ?2", nativeQuery = true)
    int updateChangeLabel(Integer changeLabel, String houseId);

    //@Query(value = "SELECT * FROM saas_community_house_user WHERE (user_id =(SELECT t.id FROM (select * from saas_app_user where (id = ?1 or enterprise_user_id = ?2) and status = 1) t) or user_id = (SELECT t1.enterprise_user_id FROM (select * from saas_app_user where (id = ?3 or enterprise_user_id = ?4) and status = 1) t1) AND community_house_id = ?5)", nativeQuery = true)
    // CommunityHouseUser findByUserIdAndStatusAndCommunityHouseId(String userId1,String userId2,String userId3,String userId4,Integer status, String houseId);
    CommunityHouseUser findByUserIdAndStatusAndCommunityHouseId(String userId1, Integer status, String houseId);

    CommunityHouseUser findByUserIdAndMobileAndStatus(String userId1, String mobile, Integer status);

    List<CommunityHouseUser> findByCommunityHouseIdIn(List<String> houseIds);

    List<CommunityHouseUser> findByCardNumberAndStatus(String cardNumber, Integer status);

    List<CommunityHouseUser> findByCardNumber(String cardNumber);

    List<CommunityHouseUser> findByIdIn(List<Long> ids);

    List<CommunityHouseUser> findByUserIdAndCommunityHouseIdAndTypeAndStatus(String userId, String houseId, int type, int status);

    List<CommunityHouseUser> findCommunityHouseUserByUserIdAndStatusAndType(String userId, int state, int type);

    List<CommunityHouseUser> findCommunityHouseUserByUserIdAndStatus(String userId, int i);

    CommunityHouseUser findByCommunityHouseIdAndStatusAndCardNumber(String communityHouseId, Integer status, String cardNumber);

    List<CommunityHouseUser> findByCommunityHouseIdInAndStatus(List<String> houseIdList, int status);
}
